package Utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import entity.CommonResult;
import entity.ResultEnum;

import java.util.Map;

public class ResultUtil {
    /** 成功且带数据---Object **/
    public static CommonResult success(Object object){
        CommonResult result = new CommonResult();
        result.setRespCode(ResultEnum.SUCCESS.getRespCode());
        result.setRespDesc(ResultEnum.SUCCESS.getRespDesc());
        result.setRespData(object);
        return result;
    }

    /** 成功且带数据---JSONObject **/
    public static CommonResult successByJSONObject(JSONObject object){
        CommonResult result = new CommonResult();
        result.setRespCode(ResultEnum.SUCCESS.getRespCode());
        result.setRespDesc(ResultEnum.SUCCESS.getRespDesc());
        result.setRespData(object);
        return result;
    }

    /** 成功且带数据---JSONArray **/
    public static CommonResult successByJSONArray(JSONArray array){
        CommonResult result = new CommonResult();
        result.setRespCode(ResultEnum.SUCCESS.getRespCode());
        result.setRespDesc(ResultEnum.SUCCESS.getRespDesc());
        result.setRespData(array);
        return result;
    }

    /** 成功且带数据---JSONArray **/
    public static CommonResult successByMap(Map map){
        CommonResult result = new CommonResult();
        result.setRespCode(ResultEnum.SUCCESS.getRespCode());
        result.setRespDesc(ResultEnum.SUCCESS.getRespDesc());
        result.setRespData(map);
        return result;
    }


    /**成功但不带数据**/
    public static CommonResult success(){
        return success("");
    }

    /**成功带自定义消息**/
    public static CommonResult success(String code,String msg){
        CommonResult result = new CommonResult();
        result.setRespCode(code);
        result.setRespDesc(msg);
        result.setRespData("");
        return result;
    }

    /**失败**/
    public static CommonResult error(String code,String msg){
        CommonResult result = new CommonResult();
        result.setRespCode(code);
        result.setRespDesc(msg);
        result.setRespData("");
        return result;
    }
    /**失败**/
    public static CommonResult error(String msg){
        CommonResult result = new CommonResult();
        result.setRespCode(ResultEnum.ERROR.getRespCode());
        result.setRespDesc(msg);
        result.setRespData("");
        return result;
    }

    /**失败
     *传入枚举
     * **/
    public static CommonResult error(ResultEnum resultEnum){
        CommonResult result = new CommonResult();
        result.setRespCode(resultEnum.getRespCode());
        result.setRespDesc(resultEnum.getRespDesc());
        result.setRespData("");
        return result;
    }
}
